home *** CD-ROM | disk | FTP | other *** search
- /*
- * SetFilePanel.c - 'SetFile Panel' Drop-In Preferences
- *
- * Copyright © 1995 metrowerks inc. All rights reserved.
- *
- */
-
- /* prevent access to dialog (for a glimpse of the future...) */
- #define CW_STRICT_DIALOGS 1
-
- /* standard headers */
- #include <stdarg.h>
- #include <stdio.h>
- #include <string.h>
-
- /* system headers */
- #include <AERegistry.h>
- #include <Drag.h>
- #include <Palettes.h>
- #include <Resources.h>
- #include <Scrap.h>
- #include <TextUtils.h>
- #include <Sound.h>
-
- /* compiler headers */
- #include <A4Stuff.h>
- #include <SetUpA4.h>
- #include <DropInPanel.h>
- #include <Files.h>
- #include <Finder.h>
-
- /* project headers */
- #include "SetFilePanel.h"
-
-
- /* current version number for our prefs data */
- #define PSAMPLEPANELVERSION 3
-
-
- enum {
- kFactoryPrefsID = 128,
- kCW7ItemListID = 128,
- kCW8ItemListID,
-
- kSetFileStringsID = 128,
- kSetFileInfoStr = 1
- };
-
-
- /* prototypes of local functions */
- static short InitDialog(PanelParameterBlock *pb);
- static void TermDialog(PanelParameterBlock *pb);
- static void PutData(PanelParameterBlock *pb, Handle options);
- static short GetData(PanelParameterBlock *pb, Handle options, Boolean noisy);
- static short Filter(PanelParameterBlock *pb, EventRecord *event, short *itemHit);
- static void HandleKey(PanelParameterBlock *pb, EventRecord *event);
- static void HandleClick(PanelParameterBlock *pb, EventRecord *event);
- static void ItemHit(PanelParameterBlock *pb);
- static void FindStatus(PanelParameterBlock *pb);
- static void ObeyCommand(PanelParameterBlock *pb);
- static void Validate(Handle original, Handle current, Boolean *recompile, Boolean *relink, Boolean *reset);
- static short GetPref(AEKeyword keyword, AEDesc *prefsDesc, Handle settings);
- static short SetPref(AEKeyword keyword, const AEDesc *prefsDesc, Handle settings);
- static short GetFactory(Handle settings);
- static short UpdatePref(Handle settings);
- static Boolean ComparePrefs(Handle prefsHand1, Handle prefsHand2);
- static void OutlineRect(const Rect* focusRect, Boolean outlineOn);
- static OSErr DragEnter(PanelParameterBlock *pb);
- static void DragWithin(PanelParameterBlock *pb);
- static void DragExit(PanelParameterBlock *pb);
- static void DragDrop(PanelParameterBlock *pb);
- static pascal void PanelDrawBoxCB(DialogPtr dp, short item);
-
-
- /*
- * main - entry-point for Drop-In Preferences Panel
- *
- */
-
- pascal short main(PanelParameterBlock *pb)
- {
- short result, theItem;
-
- EnterCodeResource();
- PrepareCallback();
-
- result = noErr;
- // DebugStr("\p SetFilePanel main");
- switch (pb->request)
- {
- case reqInitPanel:
- /* panel has just been loaded into memory */
- break;
-
- case reqTermPanel:
- /* panel is about to be unloaded from memory */
- break;
-
- case reqInitDialog:
- /* hook our dialog item list into the preferences dialog */
- result = InitDialog(pb);
- break;
-
- case reqTermDialog:
- /* unhook our dialog item list from the preferences dialog */
- TermDialog(pb);
- break;
-
- case reqPutData:
- /* put the data in the given handle into our dialog items */
- PutData(pb, pb->currentPrefs);
- break;
-
- case reqGetData:
- /* fill in the given handle with our dialog items */
- result = GetData(pb, pb->currentPrefs, true);
- break;
-
- case reqFilter:
- /* filter an event in the dialog */
- result = Filter(pb, pb->event, &pb->itemHit);
- break;
-
- case reqItemHit:
- /* handle a hit on one of our dialog items */
- ItemHit(pb);
- break;
-
- case reqDrawCustomItem:
- /* handle a request to draw one of our user items (CW/8 and later) */
- theItem = pb->itemHit - pb->baseItems;
- //switch (theItem) {
- // default:
- //}
- break;
-
- case reqActivateItem:
- theItem = pb->itemHit - pb->baseItems;
- //if (theItem == kPictItem)
- //{
- // ActivatePictBox(pb, true);
- //}
- break;
-
- case reqDeactivateItem:
- theItem = pb->itemHit - pb->baseItems;
- //if (theItem == kPictItem)
- //{
- // ActivatePictBox(pb, false);
- //}
- break;
-
- case reqHandleKey:
- theItem = pb->itemHit - pb->baseItems;
- //if (theItem == kPictItem)
- //{
- // HandleKey(pb, pb->event);
- //}
- break;
-
- case reqHandleClick:
- theItem = pb->itemHit - pb->baseItems;
- //if (theItem == kPictItem)
- //{
- // HandleClick(pb, pb->event);
- //}
- break;
-
- case reqFindStatus:
- FindStatus(pb);
- break;
-
- case reqObeyCommand:
- ObeyCommand(pb);
- break;
-
- case reqAEGetPref:
- /* return one item in the given handle as an Apple Event descriptor */
- result = GetPref(pb->prefsKeyword, &pb->prefsDesc, pb->currentPrefs);
- break;
-
- case reqAESetPref:
- /* change one item in the given handle according to the given Apple Event descriptor */
- result = SetPref(pb->prefsKeyword, &pb->prefsDesc, pb->currentPrefs);
- break;
-
- case reqValidate:
- /* determine if we need to reset paths, recompile, or relink */
- Validate(pb->originalPrefs, pb->currentPrefs, &pb->recompile, &pb->relink, &pb->reset);
- break;
-
- case reqGetFactory:
- /* return our factory settings */
- result = GetFactory(pb->factoryPrefs);
- break;
-
- case reqUpdatePref:
- /* update the given handle to use the current format for our prefs data */
- result = UpdatePref(pb->currentPrefs);
- break;
-
- default:
- result = paramErr;
- break;
- }
-
- ExitCodeResource();
-
- return (result);
- }
-
-
- /*
- * InitDialog - initialize Dialog Box items for this panel
- *
- */
-
- static short InitDialog(PanelParameterBlock *pb)
- {
- short ditlID;
- OSErr err;
-
- // The library function will call the IDE to append the dialog items
- // if possible; else it will call AppendDITL itself. This way, you
- // don't have to worry about it.
-
- if (pb->version < DROPINPANELAPIVERSION_2)
- ditlID = kCW7ItemListID;
- else
- ditlID = kCW8ItemListID;
-
- err = CWPanlAppendItems(pb, ditlID);
-
- return (err);
- }
-
- /*
- * TermDialog - destroy Dialog Box items for this panel
- *
- */
-
- static void TermDialog(PanelParameterBlock *pb)
- {
- #pragma unused (pb)
- }
-
- /*
- * PutData - copy the options data from the handle to the screen
- *
- */
-
- static void PutData(PanelParameterBlock *pb, Handle options)
- {
- SetFilePref prefsData = ** (SetFilePrefHandle) options;
- UInt8 typeStr[] = {4,0,0,0,0};
-
-
- CWPanlSetItemValue(pb, kAliasItem, prefsData.bits.alias);
- CWPanlSetItemValue(pb, kInvisibleItem, prefsData.bits.invisible);
- CWPanlSetItemValue(pb, kBundleItem, prefsData.bits.bundle);
- CWPanlSetItemValue(pb, kSystemItem, prefsData.bits.system);
- CWPanlSetItemValue(pb, kStationaryItem, prefsData.bits.stationary);
- CWPanlSetItemValue(pb, kCustomIconItem, prefsData.bits.customIcon);
- CWPanlSetItemValue(pb, kLockedItem, prefsData.lock);
- CWPanlSetItemValue(pb, kInitedItem, prefsData.bits.inited);
- CWPanlSetItemValue(pb, kNoInitsItem, prefsData.bits.noinits);
- CWPanlSetItemValue(pb, kSharedItem, prefsData.bits.shared);
- CWPanlSetItemValue(pb, kDeskTopItem, prefsData.bits.onDesk);
- CWPanlSetItemValue(pb, kRoutingItem, prefsData.xbits.routing);
- CWPanlSetItemValue(pb, kBadgeItem, prefsData.xbits.badge);
- BlockMove(&prefsData.creator,&typeStr[1],4);
- CWPanlSetItemText (pb, kCreatorItem, typeStr);
- BlockMove(&prefsData.type,&typeStr[1],4);
- CWPanlSetItemText (pb, kTypeItem, typeStr);
- if (pb->version < DROPINPANELAPIVERSION_2)
- {
- #if 0
- CWPanlActivateItem(pb, kOutFileItem);
- #endif
- }
- }
-
- /*
- * GetData - copy the options data from screen to the handle
- *
- */
-
- static short GetData(PanelParameterBlock *pb, Handle options, Boolean noisy)
- {
- SetFilePref prefsData = ** (SetFilePrefHandle) options;
- long symval;
- OSErr err;
- UInt8 typeStr[32];
-
- CWPanlGetItemValue(pb, kAliasItem, &symval);
- prefsData.bits.alias = symval;
- CWPanlGetItemValue(pb, kInvisibleItem, &symval);
- prefsData.bits.invisible = symval;
- CWPanlGetItemValue(pb, kBundleItem, &symval);
- prefsData.bits.bundle = symval;
- CWPanlGetItemValue(pb, kSystemItem, &symval);
- prefsData.bits.system = symval;
- CWPanlGetItemValue(pb, kStationaryItem, &symval);
- prefsData.bits.stationary = symval;
- CWPanlGetItemValue(pb, kCustomIconItem, &symval);
- prefsData.bits.customIcon = symval;
- CWPanlGetItemValue(pb, kLockedItem, &symval);
- prefsData.lock = symval;
- CWPanlGetItemValue(pb, kInitedItem, &symval);
- prefsData.bits.inited = symval;
- CWPanlGetItemValue(pb, kNoInitsItem, &symval);
- prefsData.bits.noinits = symval;
- CWPanlGetItemValue(pb, kSharedItem, &symval);
- prefsData.bits.shared = symval;
- CWPanlGetItemValue(pb, kDeskTopItem, &symval);
- prefsData.bits.onDesk = symval;
- CWPanlGetItemValue(pb, kRoutingItem, &symval);
- prefsData.xbits.routing = symval;
- CWPanlGetItemValue(pb, kBadgeItem, &symval);
- prefsData.xbits.badge = symval;
- err = CWPanlGetItemText (pb, kCreatorItem, typeStr,31);
- if (err != noErr) {
- // An error occurred while retrieving the text. It was probably too
- // long to fit in the given length. If we're in "noisy" mode, alert
- // the user.
-
- if (noisy)
- {
- SysBeep(10);
- CWPanlActivateItem(pb, kCreatorItem);
- return (userCanceledErr);
- }
- } else {
- BlockMove(&typeStr[1],&prefsData.creator,4);
- }
- err = CWPanlGetItemText (pb, kTypeItem, typeStr,31);
- if (err != noErr) {
- // An error occurred while retrieving the text. It was probably too
- // long to fit in the given length. If we're in "noisy" mode, alert
- // the user.
-
- if (noisy)
- {
- SysBeep(10);
- CWPanlActivateItem(pb, kTypeItem);
- return (userCanceledErr);
- }
- } else {
- BlockMove(&typeStr[1],&prefsData.type,4);
- }
- prefsData.bits.rsvd0x0200 = 0;
- prefsData.bits.rsvd0x003e = 0;
- prefsData.xbits.rsvd0xFE00 = 0;
- prefsData.xbits.rsvd0x00F8 = 0;
- prefsData.xbits.rsvd0x0003 = 0;
- ** (SetFilePrefHandle) options = prefsData;
-
- return (noErr);
- }
-
- /*
- * Filter - filter an event for the Preferences panel
- *
- */
- static short Filter(PanelParameterBlock *pb, EventRecord *event, short *itemHit)
- {
- #pragma unused(pb, event, itemHit)
-
- return (noErr);
- }
-
- /*
- * HandleKey
- *
- */
- static void HandleKey(PanelParameterBlock *pb, EventRecord *event)
- {
- #pragma unused(pb,event)
- }
-
- /*
- * HandleClick
- *
- */
- static void HandleClick(PanelParameterBlock *pb, EventRecord *event)
- {
- #pragma unused(pb,event)
- }
-
- /*
- * ItemHit - handle an itemHit in a Preferences panel
- *
- */
-
- static void ItemHit(PanelParameterBlock *pb)
- {
- short theItem = pb->itemHit - pb->baseItems;
- long oldValue;
-
- // DebugStr("\p SetFilePanel ItemHit");
- switch (theItem)
- {
- case kAliasItem:
- case kInvisibleItem:
- case kBundleItem:
- case kSystemItem:
- case kStationaryItem:
- case kCustomIconItem:
- case kLockedItem:
- case kInitedItem:
- case kNoInitsItem:
- case kSharedItem:
- case kDeskTopItem:
- case kBadgeItem:
- case kRoutingItem:
- CWPanlGetItemValue(pb, theItem, &oldValue);
- CWPanlSetItemValue(pb, theItem, !oldValue);
- break;
-
- default:
- break;
- }
-
- GetData(pb, pb->currentPrefs, false);
-
- pb->canRevert = !ComparePrefs(pb->originalPrefs, pb->currentPrefs);
- pb->canFactory = !ComparePrefs(pb->factoryPrefs, pb->currentPrefs);
- }
-
- /*
- * FindStatus
- *
- */
- static void FindStatus(PanelParameterBlock *pb)
- {
- short command = pb->itemHit;
- Boolean enabled = false;
-
- pb->itemHit = enabled;
- }
-
- /*
- * ObeyCommand
- *
- */
-
- static void ObeyCommand(PanelParameterBlock *pb)
- {
- pb->itemHit = 0;
- }
-
- /*
- * Validate - check if panel's changes require a recompile or relink
- *
- */
-
- static void Validate(Handle original, Handle current, Boolean *recompile, Boolean *relink, Boolean *reset)
- {
- *recompile = false;
- *relink = !ComparePrefs (original, current);
- *reset = false;
- }
-
- /*
- * GetPref - get a specified Preference setting for an AppleEvent request
- *
- */
-
- static short GetPref(AEKeyword keyword, AEDesc *prefsDesc, Handle settings)
- {
- SetFilePref prefsData = ** (SetFilePrefHandle) settings;
- DescType anEnum;
- OSErr err;
- #pragma unused(keyword,prefsDesc,anEnum)
- switch (keyword)
- {
-
- #if 0
- case prefsPR_ProjectType:
- switch (prefsData.projtype)
- {
- case kProjTypeApplication: anEnum = enum_Project_Application; break;
- case kProjTypeLibrary: anEnum = enum_Project_Library; break;
- case kProjTypeSharedLib: anEnum = enum_Project_SharedLibrary; break;
- case kProjTypeCodeResource: anEnum = enum_Project_CodeResource; break;
- case kProjTypeMPWTool: anEnum = enum_Project_MPWTool; break;
- default: return (paramErr);
- }
- err = AECreateDesc(typeEnumeration, &anEnum, sizeof(anEnum), prefsDesc);
- break;
-
- case prefsPR_FileName:
- err = AECreateDesc(typeChar, prefsData.outfile+1, StrLength(prefsData.outfile), prefsDesc);
- break;
- #endif
- default:
- err = errAECantHandleClass;
- break;
- }
-
- return (err);
- }
-
- /*
- * SetPref - set a specified Preference setting from an AppleEvent request
- *
- */
-
- static short SetPref(AEKeyword keyword, const AEDesc *prefsDesc, Handle settings)
- {
- SetFilePref prefsData = ** (SetFilePrefHandle) settings;
- AEDesc toDesc = { typeNull, NULL };
- OSErr err = noErr;
- Handle dataHand;
- Size textLength;
- DescType anEnum;
- #pragma unused(prefsDesc,anEnum,textLength,dataHand)
- switch (keyword)
- {
- #if 0
- case prefsLN_GenerateSymFile:
- if (prefsDesc->descriptorType == typeBoolean)
- {
- dataHand = prefsDesc->dataHandle;
- }
- else
- {
- err = AECoerceDesc(prefsDesc, typeBoolean, &toDesc);
- if (err == noErr)
- dataHand = toDesc.dataHandle;
- }
- if (err == noErr)
- {
- prefsData.linksym = ** (Boolean **) dataHand;
- }
- break;
-
- case prefsPR_ProjectType:
- if (prefsDesc->descriptorType != typeEnumeration)
- {
- err = errAETypeError;
- break;
- }
-
- anEnum = ** (DescType **) prefsDesc->dataHandle;
-
- switch (anEnum)
- {
- case enum_Project_Application: prefsData.projtype = kProjTypeApplication; break;
- case enum_Project_Library: prefsData.projtype = kProjTypeLibrary; break;
- case enum_Project_SharedLibrary: prefsData.projtype = kProjTypeSharedLib; break;
- case enum_Project_CodeResource: prefsData.projtype = kProjTypeCodeResource; break;
- case enum_Project_MPWTool: prefsData.projtype = kProjTypeMPWTool; break;
- default: return (errAECoercionFail);
- }
- break;
-
- case prefsPR_FileName:
- if (prefsDesc->descriptorType == typeChar)
- {
- dataHand = prefsDesc->dataHandle;
- }
- else
- {
- err = AECoerceDesc(prefsDesc, typeChar, &toDesc);
- if (err == noErr)
- dataHand = toDesc.dataHandle;
- }
- if (err == noErr)
- {
- textLength = GetHandleSize(dataHand);
- if (textLength > sizeof(prefsData.outfile) - 1)
- textLength = sizeof(prefsData.outfile) - 1;
- BlockMoveData(*dataHand, prefsData.outfile+1, textLength);
- prefsData.outfile[0] = textLength;
- }
- break;
- #endif
- default:
- err = errAECantHandleClass;
- break;
- }
-
- if (err == noErr)
- {
- ** (SetFilePrefHandle) settings = prefsData;
- }
-
- AEDisposeDesc(&toDesc);
-
- return (err);
- }
-
- /*
- * GetFactory - retrieve factory settings
- *
- */
-
- static short GetFactory(Handle settings)
- {
- Handle factory;
- Size size;
- OSErr err;
-
- factory = Get1Resource('pref', kFactoryPrefsID);
- if (factory == NULL) {
- err = ResError();
- if (err == noErr)
- err = resNotFound;
- return (err);
- }
-
- size = GetHandleSize(factory);
- SetHandleSize(settings, size);
- err = MemError();
-
- if (err == noErr) {
- BlockMoveData(*factory, *settings, size);
- }
-
- return (err);
- }
-
- /*
- * UpdatePref - "upgrade" a pref to the current version
- *
- */
-
- static short UpdatePref(Handle settings)
- {
- #if 0
- typedef struct SetFilePref_V1 {
- short version; // version # of pref information
- short projtype; // project type
- Str32 outfile; // output file name
- } SetFilePref_V1;
-
- typedef struct SetFilePref {
- short version; // version # of pref information
- short projtype; // project type
- Str32 outfile; // output file name
- Boolean linksym; // generate SYM file
- } SetFilePref_V2;
-
- SetFilePref_V1 v1;
- SetFilePref_V2 v2;
- #endif
- SetFilePrefHandle prefsHand = (SetFilePrefHandle) settings;
- OSErr err = noErr;
-
- switch ((*prefsHand)->version)
- {
- #if 0
- case 1:
- /* convert from version 1 to version 2 */
- v1 = ** (SetFilePref_V1 **) prefsHand;
- GetFactory(settings);
- (*prefsHand)->projtype = v1.projtype;
- BlockMoveData(v1.outfile, (*prefsHand)->outfile, sizeof(v1.outfile));
- break;
-
- case 2:
- /* convert from version 1 to version 2 */
- v2 = ** (SetFilePref_V2 **) prefsHand;
- GetFactory(settings);
- (*prefsHand)->projtype = v2.projtype;
- (*prefsHand)->linksym = v2.linksym;
- BlockMoveData(v1.outfile, (*prefsHand)->outfile, sizeof(v1.outfile));
- break;
-
- case PSAMPLEPANELVERSION:
- break;
- #endif
-
- default:
- err = kBadPrefVersion;
- break;
- }
-
- return (err);
- }
-
- /*
- * ComparePrefs
- *
- */
- static Boolean ComparePrefs(Handle prefsHand1, Handle prefsHand2)
- {
- SetFilePrefHandle prefs1 = (SetFilePrefHandle) prefsHand1;
- SetFilePrefHandle prefs2 = (SetFilePrefHandle) prefsHand2;
-
- return (
- ((*prefs1)->version == (*prefs2)->version) &&
- ((*prefs1)->creator == (*prefs2)->creator) &&
- ((*prefs1)->type == (*prefs2)->type) &&
- ((*prefs1)->bits.alias == (*prefs2)->bits.alias) &&
- ((*prefs1)->bits.invisible == (*prefs2)->bits.invisible) &&
- ((*prefs1)->bits.bundle == (*prefs2)->bits.bundle) &&
- ((*prefs1)->bits.system == (*prefs2)->bits.system) &&
- ((*prefs1)->bits.stationary == (*prefs2)->bits.stationary) &&
- ((*prefs1)->bits.customIcon == (*prefs2)->bits.customIcon) &&
- ((*prefs1)->bits.inited == (*prefs2)->bits.inited) &&
- ((*prefs1)->bits.shared == (*prefs2)->bits.shared) &&
- ((*prefs1)->bits.onDesk == (*prefs2)->bits.onDesk) &&
- ((*prefs1)->xbits.routing == (*prefs2)->xbits.routing) &&
- ((*prefs1)->xbits.badge == (*prefs2)->xbits.badge));
- }
-